package com.goods.business.service.impl;

import com.goods.business.entities.Product;
import com.goods.business.entities.ProductStock;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.business.service.ProductStockService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Decheng Kong
 * @date 2021/11/9 18:53
 */
@Service
public class ProductStockServiceImpl implements ProductStockService {
    @Autowired
    private ProductStockMapper productStockMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageVO<ProductStockVO> findProductStocks(Integer pageNum, Integer pageSize, String name, Integer[] categorys) throws BusinessException {

        List<ProductStock> productStocks = productStockMapper.selectAll();

        // 关联查询
        List<Product> products = productStocks.stream()
                .map(productStock -> {
                    Example example = new Example(Product.class);
                    example.createCriteria().andEqualTo("pNum", productStock.getPNum());
                    return productMapper.selectOneByExample(example);
                })
                .collect(Collectors.toList());

        // 移除空元素, 空元素产生的原因是库存表存在某个商品的库存量，但是商品表不存在该商品的信息
        products.removeIf(Objects::isNull);

        // 条件查询
        if (categorys != null && categorys.length > 0) {
            // 分类级别
            int level = categorys.length;
            switch (level) {
                case 1:
                    products = products.stream().filter(product -> product.getOneCategoryId().intValue() == categorys[level - 1]).collect(Collectors.toList());
                    break;
                case 2:
                    products = products.stream().filter(product -> product.getTwoCategoryId().intValue() == categorys[level - 1]).collect(Collectors.toList());
                    break;
                case 3:
                    products = products.stream().filter(product -> {
                        Long threeCategoryId = product.getThreeCategoryId();
                        Integer category = categorys[level - 1];
                        return threeCategoryId.intValue() == category;
                    }).collect(Collectors.toList());
                    break;
                default:
                    throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR);
            }

        }

        // 模糊查询
        if (!StringUtils.isEmpty(name)) {
            products = products.stream().filter(product -> product.getName().contains(name)).collect(Collectors.toList());
        }

        // 封装数据
        List<ProductStockVO> productStockVOS = products.stream()
                .map(product -> {
                    ProductStockVO productStockVO = new ProductStockVO();
                    BeanUtils.copyProperties(product, productStockVO);
                    Example example = new Example(ProductStock.class);
                    example.createCriteria().andEqualTo("pNum", product.getPNum());
                    ProductStock pNum = productStockMapper.selectOneByExample(example);
                    productStockVO.setStock(pNum.getStock());
                    return productStockVO;
                }).collect(Collectors.toList());

        // 分页
        productStockVOS = productStockVOS.stream()
                .skip((long) (pageNum - 1) * pageSize)
                .limit(pageSize)
                .collect(Collectors.toList());

        return new PageVO<>(productStockVOS.size(), productStockVOS);
    }
}
